﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.IO.IsolatedStorage;
using System.ComponentModel;

namespace AirportApp
{
    public class Settings : INotifyPropertyChanged
    {
        public Settings()
        {

        }

        #region Internal Helper

        private void SetValue(string name, object v)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(name))
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(name);
            }
            IsolatedStorageSettings.ApplicationSettings.Add(name, v);
            IsolatedStorageSettings.ApplicationSettings.Save();

            Update(name);
        }

        private object GetValue(string name, object defaultValue)
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return defaultValue;
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(name))
            {
                return IsolatedStorageSettings.ApplicationSettings[name];
            }
            else
            {
                return defaultValue;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void Update(string name)
        {
            OnPropertyChanged(name);

            // Calculated Values 
            OnPropertyChanged("BMI");
            OnPropertyChanged("PerfectRangeMin");
            OnPropertyChanged("PerfectRangeMax");
            OnPropertyChanged("PerfectValue");
            OnPropertyChanged("DiffToPerfect");
            OnPropertyChanged("DiffToLower");
            OnPropertyChanged("DiffToHigher");
        }

        #endregion

        #region Base Values

        public bool SearchAllText
        {
            get 
            { 
                object v = GetValue("SearchAllText", true);
                return (bool)v;
            }
            set { SetValue("SearchAllText", value); }
        }

        #endregion

        
    }
}
